package sdc.media.motorbike.Model;

import sdc.media.motorbike.Controller.Assets;
import sdc.media.motorbike.Controller.Constant;
import sdc.media.motorbike.Controller.WorldGame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

public class Car extends Object {

	private float sizeX, sizeY;
	public float scaleInit;
	public int type;
	public float time_to_turn;
	public boolean isMove = false;
	public int direction = -1;
	
	public Car() {
		// TODO Auto-generated constructor stub
		super();
	}
	
	public void init(World world, String name, Vector2 position,int type) {
		// TODO Auto-generated method stub
		super.init(world, name, position);
		// TODO Auto-generated constructor stub
		this.type = type;
		scaleInit = 0.01f;
		time_to_turn = 0;
		switch (type) {
		case 0:
			setImage(Assets.enemy1);
			break;
		case 1:
			setImage(Assets.enemy1);
			break;
		case 2:
			setImage(Assets.enemy2);
			break;
		case 3:
			setImage(Assets.enemy3);
			break;
		case 4:
			setImage(Assets.enemy3);
			break;
		default:
			break;
		}
		
		setSprite(new Sprite(getImage()));
		getSprite().setScale(scaleInit, scaleInit);
		setBody(world.createBody(getBodyDef()));
		scaleBody();
		getBody().setFixedRotation(true);
		loadSpriteToBody();
	}

	public void update(WorldGame world, float deltaTime) {
		// TODO Auto-generated method stub
		float ti_le = Gdx.graphics.getWidth() / 3.0f / Gdx.graphics.getHeight();
		float Vx = 80 * Gdx.graphics.getWidth() / 800 * ti_le * world.speed * 1.5f;
		float Vy = -180 * Gdx.graphics.getHeight() / 480 * ti_le * world.speed * 1.5f;
		//float V0 = -70 * Gdx.graphics.getHeight() / 480 * ti_le * world.speed;
		if(type == 0) {
			if((getBody().getPosition().y - world.motorbike.getBody().getPosition().y) > world.HEIGHT * 0.6f) {
				getBody().setLinearVelocity(0, Vy);
				direction = 1;
			} else {
				direction = -1;
				time_to_turn += deltaTime * world.speed;
				if(time_to_turn <= 2f)
					getBody().setLinearVelocity(-world.WIDTH / 2, Vy);
			}
		} else if(type == 1) {
			if((getBody().getPosition().y - world.motorbike.getBody().getPosition().y) > world.HEIGHT * 0.6f) {
				getBody().setLinearVelocity(0, Vy);
				direction = 2;
			} else {
				direction = -1;
				time_to_turn += deltaTime * world.speed;
				if(time_to_turn <= 2f)
					getBody().setLinearVelocity(world.WIDTH / 2, Vy);
			}
		} else if(type == 2) {
			if((getBody().getPosition().y - world.motorbike.getBody().getPosition().y) > world.HEIGHT * 0.6f) {
				getBody().setLinearVelocity(- Vx, Vy);
				direction = 2;
			} else {
				direction = -1;
				time_to_turn += deltaTime * world.speed;
				if(time_to_turn <= 2f)
					getBody().setLinearVelocity(world.WIDTH / 4, Vy);
			}
		} else if(type == 3){
			
			if((getBody().getPosition().y - world.motorbike.getBody().getPosition().y) > world.HEIGHT * 0.6f) {
				getBody().setLinearVelocity(Vx, Vy);
				direction = 1;
			} else {
				direction = -1;
				time_to_turn += deltaTime * world.speed;
				if(time_to_turn <= 2f)
					getBody().setLinearVelocity(-world.WIDTH / 4, Vy);
			}
		} else {
			if(getBody().getPosition().x < 0)
				getBody().setLinearVelocity(-Vx, Vy);
			else if(getBody().getPosition().x > 0)
				getBody().setLinearVelocity(Vx, Vy);
			else
				getBody().setLinearVelocity(0, Vy);
			direction = -1;
		}
		
		if(getBody().getPosition().y - getHeight() / Constant.PIXEL_PER_METER / 2 <= -world.HEIGHT / 2 + 2) {
			setState(Car.STATE.DIE);
			getBody().setActive(false);
			//world.worldBox2D.destroyBody(getBody());
		}
		
		// Scale Body
		for (int i = 0; i < getBody().getFixtureList().size(); i++) {
			getBody().destroyFixture(getBody().getFixtureList().get(i));
		}
		scaleBody();
	}
	
	@Override
	public void reset() {
		// TODO Auto-generated method stub
		super.reset();
	}
	
	public void scaleBody() {
		// TODO Auto-generated method stub
		// scale body
		sizeX = getSprite().getScaleX();
		sizeY = getSprite().getScaleY();
		
		PolygonShape newShape = new PolygonShape();
		newShape.setAsBox((getWidth()-10) / (2.0f * Constant.PIXEL_PER_METER) * sizeX, (getHeight()-10) / (2.0f * Constant.PIXEL_PER_METER) * sizeY);
		getFixtureDef().shape = newShape;
		getBody().createFixture(getFixtureDef());
		newShape.dispose();
	}
}
